package com.wandian.notepad.tools;


import android.util.Log;

import com.mylib.libcore.utils.JsonUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * Created by wbb on 2016/5/25.
 * <p>
 * 加密解密类
 */
public class JmTools {

    private static String encryptKey = "@#-*￥cxmx^&+#@*";//加密的key
    private static String signKEY = "weizhisign44444";//签名的KEY

    /**
     * 输入加密 T
     *
     * @param str
     * @return
     */
    public static String DKAETYA16(String str) {
        String a = str.substring(0, 16);
        return a;
    }

    /**
     * 加密方法
     * 加密参数
     *
     * @param hashMap
     * @return
     */
    public static HashMap<String, String> encryptParams(HashMap<String, String> hashMap, String service) {
        long time = System.currentTimeMillis()/1000;
        //公用参数
        hashMap.put("os", "1");
        hashMap.put("version", "6.5.4");
        hashMap.put("pid", "4");


        hashMap.put("service", service);
        hashMap.put("t", time + "");
        Log.d("jiamiDaM1", service+"="+ JsonUtil.toJson(hashMap) +"");
        String s = JmTools.NRJM(hashMap, service);
        hashMap.put("s", s);
        Iterator iter = hashMap.keySet().iterator();

        Log.d("jiamiDaM", JsonUtil.toJson(hashMap) +"");


        HashMap<String, String> hashMap1 = new HashMap<>();
        Log.d("toJSONString",JsonUtil.toJson(hashMap)+"");
        String str = JmTools.encryptionEnhanced(time + "", JsonUtil.toJson(hashMap));
        hashMap1.put("data", str);
        hashMap1.put("T", time + "");
        Log.d("jiamiDa2",service+"="+JsonUtil.toJson(hashMap1) +"");
//        String result = JmTools.encryptionEnhanced(time + "", jsonObject.toJSONString());
//        String t = time+"";
//        String keyString = Security.md5(t + encryptKey);
//        String key = DKAETYA16(keyString);
//        //解密
//        String data = Security.decrypt(key, result);
//        Logger.i("=======[解密 wbb]========:"+data);

//        for (Map.Entry<String, String> entry : hashMap1.entrySet()) {
            Log.e("http","&data=" + str + "&T=" + time);
//        }
        return hashMap1;
    }



    /**
     * 字符串 加密
     *
     * @param time
     * @param mString
     * @return
     */
    public static String encryptionEnhanced(String time, String mString) {

        String key = Security.md5(time + encryptKey);

        String key16 = DKAETYA16(key);

        String date = Security.encrypt(key16, mString);

        return date;
    }

    /**
     * 加密 url signkey
     *
     * @param hashMap
     * @param method
     * @return
     */
    public static String NRJM(HashMap<String, String> hashMap, String method) {
        List<String> listKey = new ArrayList();
        Iterator it = hashMap.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next().toString();
            listKey.add(key);
        }

        List<String> listSort = Sorting.listSort(listKey);
        String key = "";
        StringBuilder sb = new StringBuilder();
        for (String string : listSort) {
            String name = hashMap.get(string);
            key = sb.append(name).toString();
        }
        return Security.md5(key + signKEY);
    }


}
